<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge, chrome=1">
    <meta name="renderer" content="webkit">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <meta http-equiv="Cache-Control" content="no-transform"/>
    <meta name="referrer" content="no-referrer" />
    <meta name="referrer" content="never">
    <meta name="referrer" content="same-origin" />
    <title>后台管理系统</title>
    <!--公共模块-->
    <div th:replace="~{layout}"></div>
    <script th:src="@{/static/js/home.js}"></script>
    <link rel="stylesheet" th:href="@{/static/css/home.css}">
    <style id="layuimini-bg-color">
    </style>
</head>
<!--参考 layui 后台布局-->
<body class="layui-layout-body">
<div class="layui-layout layui-layout-admin">
    <div class="layui-header">
        <div class="layui-logo">Springboot-Manager</div>

        <a>
            <div class="layuimini-tool-left" onclick="iconHide()"><i
                    class="icon-color layui-icon layui-icon-shrink-right" style="color:white" id="hide"></i></div>
        </a>

        <!-- 头部区域（可配合layui已有的水平导航） -->
        <ul class="layui-nav layui-layout-right">
            <li class="layui-nav-item" lay-unselect>
                <a href="javascript:;" data-refresh="刷新"><i class="layui-icon layui-icon-refresh"></i></a>
            </li>
            <li class="layui-nav-item " lay-unselect>
                <a href="javascript:;" data-check-screen="full"><i class="layui-icon layui-icon-screen-full"></i></a>
            </li>
            <li class="layui-nav-item">
                <a href="javascript:;">
                    <div><span id="nickName">guest</span></div>
                </a>
                <dl class="layui-nav-child">
                    <dd><a id="userProper" href="javascript:;" data-id="userProper" data-title="基本资料"
                           data-url="/index/users/info"
                           class="menuNvaBar">基本资料</a></dd>
                    <dd><a id="userSafe" href="javascript:;" data-id="userSafe" data-title="安全设置"
                           data-url="/index/users/password"
                           class="menuNvaBar">更换密码</a></dd>
                    <dd><a href="javascript:;" id="logout">退出</a></dd>
                </dl>
            </li>
            <li class="layui-nav-item layuimini-select-bgcolor mobile layui-hide-xs" lay-unselect>
                <a href="javascript:;" data-bgcolor="配色方案"><i class="layui-icon layui-icon-more-vertical"></i></a>
            </li>
        </ul>
    </div>
    <!-- 左侧菜单-->
    <div id="mainmenu" class="layui-side layui-bg-black">
        <div class="layui-side-scroll">
            <div class="navBar layui-side-scroll" id="navBarId">

            </div>
        </div>
    </div>
    <div class="layui-body">
        <!-- 内容主体区域 -->
        <div class="layui-tab" lay-filter="tab" lay-allowclose="true">
            <ul class="layui-tab-title" style="width: 90%">
                <li class="first-tab layui-this"><i class="layui-icon layui-icon-home"></i> <span>主页</span></li>
            </ul>
            <ul class="layui-nav closeBox">
                <li class="layui-nav-item">
                    <a href="javascript:;"> <i class="layui-icon layui-icon-circle-dot"></i> 页面操作</a>
                    <dl class="layui-nav-child">
                        <dd><a href="javascript:;" data-page-close="other"><i class="layui-icon layui-icon-close"></i>
                            关闭其他</a></dd>
                        <dd><a href="javascript:;" data-page-close="all"><i
                                class="layui-icon layui-icon-close-fill"></i> 关闭全部</a></dd>
                    </dl>
                </li>
            </ul>
            <div class="layui-tab-content">
                <div class="layui-tab-item layui-show">
                    <iframe th:src="@{/index/main}" frameborder="0" name="content" width="100%" id="home"></iframe>
                </div>
            </div>
        </div>
    </div>
</div>
</body>

<script>
    var layer = layui.layer;
    var form = layui.form;
    var element = layui.element;
    $("#userProper").attr("data-url", ctx + "index/users/info")
    $("#userSafe").attr("data-url", ctx + "index/users/password")
    initBgColor();
    $(function () {
        FrameWH();
        //递归获取导航栏无线层级
        //递归获取导航栏无线层级
        var getChild = function (item) {
            let ulHtml = '<dl class="layui-nav-child">';
            item.forEach(child => {
                //判断url是不是http开头
                let url = ctx + child.url;
                if ((child.url).startsWith('http')) {
                    url = child.url;
                }
                let iconHtml = child.icon ? `<i class="layui-icon ${child.icon}"></i>` : '';
                let childHtml = '';

                if (child.children && child.children.length > 0) {
                    childHtml = `<dd><a>${iconHtml}<cite>${child.title}</cite></a>${getChild(child.children)}</dd>`;
                } else {
                    let targetAttr = child.target == "_blank" ? ' target="_blank"' : '';
                    let dataAttrs = `data-icon="${child.icon}" data-url="${url}" data-title="${child.title}" data-id="${child.id}"`;
                    //如果target是_blank，直接a href
                    if (child.target == "_blank") {
                        childHtml = `<dd><a href="${url}" class="menuNvaBar"${targetAttr}${dataAttrs}>${iconHtml}<cite>${child.title}</cite></a></dd>`;
                    } else {
                        childHtml = `<dd><a href="javascript:;" class="menuNvaBar"${targetAttr}${dataAttrs}>${iconHtml}<cite>${child.title}</cite></a></dd>`;
                    }
                }
                ulHtml += childHtml;
            });
            ulHtml += "</dl>";
            return ulHtml;
        };

        CoreUtil.sendGet(ctx + "sys/home/", null, function (res) {
            $("#deptName").html("所属部门: " + res.data.userInfo.deptName);
            $("#nickName").html(res.data.userInfo.username);
            let data = res.data.menus;
            if (data && data.length > 0) {
                let ulHtml = '<ul class="layui-nav layui-nav-tree layui-left-nav" onclick="ulHide()">';
                data.forEach((item, index) => {
                    let itemClass = index === 0 ? 'layui-nav-item layui-nav-itemed' : 'layui-nav-item';
                    let iconHtml = item.icon ? `<i class="layui-icon ${item.icon}"></i>` : '';
                    ulHtml += `<li class="${itemClass}"><a href="javascript:;">${iconHtml}<cite> ${item.title}</cite></a>`;
                    if (item.children && item.children.length > 0) {
                        ulHtml += getChild(item.children);
                    }
                    ulHtml += '</li>';
                });
                ulHtml += '</ul>';
                $("#navBarId").html(ulHtml);
                element.init();  //初始化页面元素
            } else {
                $("#navBarId").empty();
            }

            top.layer.closeAll();
        });
        $(document).on('click', '.menuNvaBar', function () {
            var dataid = $(this);
            // 如果target是_blank，直接返回
            if (dataid.attr("target") === "_blank") {
                return;
            }
            if ($(".layui-tab-title li[lay-id]").length <= 0) {
                var title = CoreUtil.isEmpty(dataid.attr("data-icon")) ? `<span>${dataid.attr("data-title")}</span>` : `<i class="layui-icon ${dataid.attr("data-icon")}"></i> <span>${dataid.attr("data-title")}</span>`;
                active.tabAdd(dataid.attr("data-url"), dataid.attr("data-id"), title);
            } else {
                var isData = false;
                $(".layui-tab-title li[lay-id]").each(function () {
                    if ($(this).attr("lay-id") == dataid.attr("data-id")) {
                        isData = true;
                    }
                });
                if (!isData) {
                    var title = CoreUtil.isEmpty(dataid.attr("data-icon")) ? `<span>${dataid.attr("data-title")}</span>` : `<i class="layui-icon ${dataid.attr("data-icon")}"></i> <span>${dataid.attr("data-title")}</span>`;
                    active.tabAdd(dataid.attr("data-url"), dataid.attr("data-id"), title);
                }
            }
            active.tabChange(dataid.attr("data-id"));
        });
        //推出登录
        $("#logout").click(function () {
            logoutDialog();
        });
    });
    //删除前确认对话框
    var logoutDialog = function () {
        layer.open({
            content: '确定要退出登录么？',
            yes: function (index, layero) {
                layer.close(index); //如果设定了yes回调，需进行手工关闭
                CoreUtil.sendGet(ctx + "sys/user/logout", null, function (res) {
                    layui.data('LocalData', null);
                    top.window.location.href = ctx + "index/login";
                });
            }
        });
    }
    //触发事件
    var active = {
        tabAdd: function (url, id, title) {
            if (url != "" && url != null && url != undefined) {
                element.tabAdd('tab', {
                    title: title
                    ,
                    content: '<iframe data-frameid="' + id + '" frameborder="0" name="content" width="100%" src="' + url + '"></iframe>'
                    ,
                    id: id
                })
            }
            FrameWH();//计算框架高度
        },
        tabChange: function (id) {
            //切换到指定Tab项
            element.tabChange('tab', id); //切换到：用户管理
            $("iframe[data-frameid='" + id + "']").attr("src", $("iframe[data-frameid='" + id + "']").attr("src"))//切换后刷新框架
        },
        tabDelete: function (id) {
            element.tabDelete("tab", id);//删除
        }
    };

    function FrameWH() {
        var h = $(window).height() - 41 - 10 - 35 - 10 - 44 - 10;
        $("iframe").css("height", h + "px");
    };


</script>

</html>